package ihistoryApp.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import ihistoryApp.dal.entity.Achievement;
import ihistoryApp.dal.entity.Title;
import ihistoryApp.dal.entity.User;
import ihistoryApp.dal.entity.UserTitle;
import ihistoryApp.dal.mapper.AchievementMapper;
import ihistoryApp.dal.mapper.TitleMapper;
import ihistoryApp.dal.mapper.UserMapper;
import ihistoryApp.dal.mapper.UserTitleMapper;
import ihistoryApp.request.ConferTitleRequest;
import ihistoryApp.request.GetTitleListRequest;
import ihistoryApp.request.SetShowTitleRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 76004
 * @apiNote
 * @date 2024/5/24 20:50
 */
@Service
@Slf4j
public class AchievementService {
    @Autowired
    private AchievementMapper achievementMapper;
    @Autowired
    private TitleMapper titleMapper;
    @Autowired
    private UserMapper userMapper;


    public String confer(ConferTitleRequest request) {
        LambdaQueryWrapper<Title> titleQuery = new LambdaQueryWrapper<Title>()
                .eq(Title::getTitleId, request.getTitleId())
                ;
        Long count = titleMapper.selectCount(titleQuery);
        if (count == 0) {
            return "title不存在";
        }
        Achievement achievement = new Achievement();
        achievement.setTitleId(request.getTitleId());
        achievement.setUid(request.getUid());
        achievementMapper.insert(achievement);
        return "操作成功";
    }

    public List<Title> getTitleList(GetTitleListRequest request) {
        LambdaQueryWrapper<Achievement> achievementQuery = new LambdaQueryWrapper<Achievement>()
                .eq(Achievement::getUid, request.getUid())
                ;
        List<Long> titleIdList = achievementMapper.selectList(achievementQuery).stream().map(Achievement::getTitleId).toList();
        if (titleIdList.isEmpty()) {
            return new ArrayList<>();
        }
        List<Title> titleList = titleMapper.selectBatchIds(titleIdList);
        return titleList;
    }

    public String setShowTitle(SetShowTitleRequest request) {
        LambdaQueryWrapper<Achievement> achievementLambdaQueryWrapper = new LambdaQueryWrapper<Achievement>()
                .eq(Achievement::getTitleId, request.getTitleId())
                .eq(Achievement::getUid, request.getUid())
                ;
        Long count = achievementMapper.selectCount(achievementLambdaQueryWrapper);
        if (count == 0) {
            return "该用户没有这个title";
        }
        Title title = titleMapper.selectById(request.getTitleId());
        if (Objects.isNull(title)) {
            return "title不存在";
        }
        LambdaUpdateWrapper<User> userLambdaQueryWrapper = new LambdaUpdateWrapper<User>()
                .eq(User::getUid, request.getUid())
                .set(User::getTitleId, request.getTitleId())
                ;
        userMapper.update(null, userLambdaQueryWrapper);
        return "操作成功";
    }

}
